package util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;

/**
 * 登入
 */
public class Login {
    public static void main(String[] args) throws Exception {
        //键盘输入用户名和密码
        Scanner s=new Scanner(System.in);
        System.out.println("请输入用户名：");
        String name=s.next();
        System.out.println("请输入密码：");
        String password=s.next();

        Properties properties = new Properties();
        //把druid.properties.properties中的内容复制到properties对象中
        properties.load(new FileInputStream("D:\\study\\Java\\YuQue\\src\\druid.properties.properties"));
        //dataSource 就是我们的连接池
        DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
        //从连接池中获取连接
        Connection connection = dataSource.getConnection();

        //sql语句
        String str="select * from user where name=?";
        //拉数据
        PreparedStatement preparedStatement = connection.prepareStatement(str);
        //给问号赋值
        preparedStatement.setString(1,name);
        //查询sql语句
        ResultSet resultSet = preparedStatement.executeQuery();
        //查询一条用if
        if(resultSet.next()){
            String password1 = resultSet.getString("password");
            if(password.equals(password1)){
                System.out.println("登入成功");
            }else {
                System.out.println("密码错误");
            }
        }else {
            System.out.println("用户名错误");
        }
        //关闭连接
        connection.close();
    }
}
